package com.allianz.webapps.secondController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oracle.net.aso.n;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.frame.utils.EInvoiceMail;
import com.allianz.webapps.common.CustomerContextHolder;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.service.AcceptingService;
import com.allianz.webapps.service.BeforeUnderwritingService;
import com.allianz.webapps.service.ClaimService;
import com.allianz.webapps.service.MyPolicyService;
import com.allianz.webapps.service.OcpPolicyContractsService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 我的保单controller层
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "myPolicy")
public class MyPolicyController {
	
	@Autowired
	private MyPolicyService myPolicyService;
	
	@Autowired
	private BeforeUnderwritingService beforeUnderwritingService;
	
	@Autowired
	private ClaimService claimService;
	
	@Autowired
	private AcceptingService acceptingService;
	
	@Autowired
	private OcpPolicyContractsService contractsService;

	@Autowired
	private ObjectMapper objectMapper;
	
	/**
	 * 获取理赔照会信息
	 * @param jsonParam = {"noticeId":2}
	 * @return json
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getClaimNote.action  
	 */
	@RequestMapping(value = "getClaimNote")
	@ResponseBody
	public List<Map<String, Object>> getClaimNote(@RequestParam(value = "jsonParam") String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> noticeMap = objectMapper.readValue(jsonParam, Map.class);
		try{
			return myPolicyService.getClaimNote(noticeMap);
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取理赔照会信息失败,请重试");
		}
	}
	
	/**
	 * 获取理赔保单列表
	 * @param jsonParam = {"agentId":4677}
	 * @return json
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getClaims.action
	 */
	@RequestMapping(value = "getClaims")
	@ResponseBody
	public List<Map<String, Object>> getClaims(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		try{
			return myPolicyService.getClaims(Long.parseLong(user.getAgentId()));
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取理赔保单列表信息失败,请重试");
		}
	}
	
	/**
	 * 获取新单追踪回执状态
	 * @param jsonParam = {"agentId":4677}
	 * @return json
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getReceipts.action
	 */
	@RequestMapping(value = "getReceipts")
	@ResponseBody
	public List<Map<String, Object>> getReceipts(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		try{
			return myPolicyService.getReceipts(Long.parseLong(user.getAgentId()));
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取新单追踪回执状态信息失败,请重试");
		}
	}
	
	/**
	 * 获取新单追踪质检状态
	 * @param jsonParam = {"agentId":4677}
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getQualities.action
	 */
	@RequestMapping(value = "getQualities")
	@ResponseBody
	public List<Map<String, Object>> getQualities(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		try{
			return myPolicyService.getQualities(Long.parseLong(user.getAgentId()));
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取新单追踪质检状态信息失败,请重试");
		}
	}
	
	/**
	 * 获取新单追踪回访状态
	 * @param jsonParam = {"agentId":4677}
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getVisitlist.action
	 */
	@RequestMapping(value = "getVisitlist")
	@ResponseBody
	public List<Map<String, Object>> getVisitlist(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		try{
			return myPolicyService.getVisitlist(Long.parseLong(user.getAgentId()));
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取新单追踪回访状态信息失败,请重试");
		}
	}
	
	/**
	 * 查询新单追踪信息
	 * @param jsonParam = {"agentId":4677}
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getNewOrders.action
	 */
	@RequestMapping(value = "getNewOrders")
	@ResponseBody
	public List<Map<String, Object>> getNewOrders(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		try{
			return myPolicyService.getNewOrders(Long.parseLong(user.getAgentId()));
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取新单追踪信息失败,请重试");
		}
	}
	
	/**
	 * 获取新单追踪保单数量
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getNewOrdersNum.action
	 */
	@RequestMapping(value = "getNewOrdersNum")
	@ResponseBody
	public Map<String, Object> getNewOrdersNum(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> result = myPolicyService.getNewOrdersNum(Long.parseLong(user.getAgentId()));
			int sum = 0;
			for(Map<String, Object> map : result) {
				sum += Integer.parseInt(map.get("NUM").toString());
			}
			resultMap.put("number", sum);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取新单追踪保单数量失败,请重试");
		}
	}
	
	/**
	 * 获取保单状态
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getPolicyStatus.action
	 */
	@RequestMapping(value = "getPolicyStatus")
	@ResponseBody
	public List<Map<String, Object>> getPolicyStatus() throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		try{
			return myPolicyService.getPolicyStatus();
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取保单状态信息失败,请重试");
		}
	}
	
	/**
	 * 获取保单信息
	 * @param jsonParam
	 * @return
	 * @throws Exception 
	 * URL:/pad_marketing/myPolicy/getWarranty.action
	 */
	@RequestMapping(value = "getWarranty")
	@ResponseBody
	public List<Map<String, Object>> getWarranty(@RequestParam(value = "jsonParam") String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try {
			return myPolicyService.getWarranty(Long.parseLong(map.get("contractId").toString()));
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取报保单信息失败,请重试");
		}
	}
	
	/**
	 * 按条件查询保单
	 * @param jsonParam
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/queryPolicies.action
	 */
	@RequestMapping(value = "queryPolicies")
	@ResponseBody
	public List<Map<String, Object>> queryPolicies(@RequestParam(value = "jsonParam") String jsonParam, HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		GregorianCalendar gc = new GregorianCalendar();
		if(map.get("inceptionFrom").toString() != null && !"".equals(map.get("inceptionFrom").toString().trim())
		&& map.get("inceptionTo").toString() != null && !"".equals(map.get("inceptionTo").toString().trim())) {
			Date inceptionFrom = sdf.parse(map.get("inceptionFrom").toString());
			Date inceptionTo = sdf.parse(map.get("inceptionTo").toString());
			if(inceptionFrom.getTime() > inceptionTo.getTime()) {
				throw new Exception("生效日期的起始日期不能大于截止日期");
			} else {
				gc.setTime(inceptionFrom);
				gc.add(5, 90);
				inceptionFrom = gc.getTime();
				if(inceptionFrom.getTime() < inceptionTo.getTime()) {
					throw new Exception("生效日期查询区间不能超过90天");
				}
			}
		}
		if(map.get("entryFrom").toString() != null && !"".equals(map.get("entryFrom").toString().trim())
		&& map.get("entryTo").toString() != null && !"".equals(map.get("entryTo").toString().trim())) {
			Date entryFrom = sdf.parse(map.get("entryFrom").toString());
			Date entryTo = sdf.parse(map.get("entryTo").toString());
			if(entryFrom.getTime() > entryTo.getTime()) {
				throw new Exception("受理日期的起始日期不能大于截止日期");
			} else {
				gc.setTime(entryFrom);
				gc.add(5, 90);
				entryFrom = gc.getTime();
				if(entryFrom.getTime() < entryTo.getTime()) {
					throw new Exception("受理日期查询区间不能超过90天");
				}
			}
		}
		User user = (User) session.getAttribute("username");
		map.put("agentId", Long.parseLong(user.getAgentId()));
		try{
			List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
			Map<String, Object> temp = new HashMap<String, Object>();
			temp.put("CONTRACT_ID", "4691887");
			temp.put("POLICY_REF", "000002924433");
			temp.put("POLICY_STATUS", "有效保单");
			temp.put("PH_NAME", "37353583735358 3735358");
			temp.put("INS_NAMES", "4043243 4043243");
			temp.put("COVER_CODE", "ASXYPCA01");
			temp.put("REGULAR_PREM", "0");
			temp.put("SOURCE_NAME", "tmall销售保单");
			result.add(temp);
			List<Map<String, Object>> ret = myPolicyService.queryPolicies(map);
			return ret;
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("查询保单列表信息失败,请重试");
		}
	}
	
	/**
	 * 获取所有保单的数量
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getPoliciesNum.action
	 */
	@RequestMapping(value = "getPoliciesNum")
	@ResponseBody
	public Map<String, Object> getPoliciesNum(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> result = myPolicyService.getPoliciesNum(Long.parseLong(user.getAgentId()));
			int sum = 0;
			for(Map<String, Object> map : result) {
				sum += Integer.parseInt(map.get("NUM").toString());
			}
			resultMap.put("number", sum);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取所有保单的数量失败,请重试");
		}
	}
	
	/**
	 * 获取保单价值信息
	 * @param jsonParam = {"contractId":262499}
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getPolicyValue.action
	 */
	@RequestMapping(value = "getPolicyValue")
	@ResponseBody
	public List<Map<String, Object>> getPolicyValue(@RequestParam(value = "jsonParam") String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try{
			return myPolicyService.getPolicyValue(map);
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取保单价值信息失败,请重试");
		}
	}
	
	/**
	 * 获取客户信息
	 * @param jsonParam = {"contractId":262499}
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getClientInfo.action
	 */
	@RequestMapping(value = "getClientInfo")
	@ResponseBody
	public List<Map<String, Object>> getClientInfo(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try{
			return myPolicyService.getClientInfo(map);
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取客户信息失败,请重试");
		}
	}
	
	/**
	 * 获取险种信息
	 * @param jsonParam = {"contractId":262499}
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getInsurances.action
	 */
	@RequestMapping(value = "getInsurances")
	@ResponseBody
	public List<Map<String, Object>> getInsurances(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try{
			return myPolicyService.getInsurances(map);
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取险种信息失败,请重试");
		}
	}
	
	/**
	 * 获取收付费信息
	 * @param jsonParam = {"contractId":262499}
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getExpenses.action
	 */
	@RequestMapping(value = "getExpenses")
	@ResponseBody
	public List<Map<String, Object>> getExpenses(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try{
			return myPolicyService.getExpenses(Long.parseLong(map.get("contractId").toString()));
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取收付费信息失败,请重试");
		}
	}
	
	/**
	 * 判断投连账户信息TAB页是否需要显示
	 * @param jsonParam = {"contractId":262499}
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/isDisplay.action
	 */
	@RequestMapping(value = "isDisplay")
	@ResponseBody
	public List<Map<String, Object>> isDisplay(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try{
			return myPolicyService.isDisplay(Long.parseLong(map.get("contractId").toString()));
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取失败,请重试");
		}
	}
	
	/**
	 * 获取投连账户信息
	 * @param jsonParam = {"contractId":262499}
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getAccounts.action
	 */
	@RequestMapping(value = "getAccounts")
	@ResponseBody
	public List<Map<String, Object>> getAccounts(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try{
			return myPolicyService.getAccounts(Long.parseLong(map.get("contractId").toString()));
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取投连账户信息失败,请重试");
		}
	}
	
	/**
	 * 获取保单理赔记录信息
	 * @param jsonParam = {"contractId":262499}
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getPolicyClaim.action
	 */
	@RequestMapping(value = "getPolicyClaim")
	@ResponseBody
	public List<Map<String, Object>> getPolicyClaim(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try{
			return myPolicyService.getPolicyClaim(Long.parseLong(map.get("contractId").toString()));
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取保单理赔记录信息失败,请重试");
		}
	}
	
	/**
	 * 获取新单追踪图表信息
	 * @param session
	 * @return
	 * @throws Exception
	 * /pad_marketing/myPolicy/getNewOrdersChart.action
	 */
	@RequestMapping(value = "getNewOrdersChart")
	@ResponseBody
	public List<Map<String, Object>> getNewOrdersChart(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		try{
			return myPolicyService.getNewOrdersChart(Long.parseLong(user.getAgentId()));
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取新单追踪图表信息失败,请重试");
		}
	}
	
	/**
	 * 获取各种保单的数量
	 * @param session
	 * @return
	 * @throws Exception 
	 * URL:/pad_marketing/myPolicy/getNumbers.action
	 */
	@RequestMapping(value = "getNumbers")
	@ResponseBody
	public Map<String, Object> getNumbers(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//新单追踪
			List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
			resultMap.put("newOrdersNum", myPolicyService.getOrdersNum(Long.parseLong(user.getAgentId())));
			
			//承保前
			resultMap.put("underwriteNum", beforeUnderwritingService.getUnderwriteNums(Long.parseLong(user.getAgentId())));
			
			//理赔
			int sum = 0;
		 	result = claimService.getClaimChart(Long.parseLong(user.getAgentId()));
		 	for(Map<String, Object> map : result) {
				sum += Integer.parseInt(map.get("NUM").toString());
			}
		 	resultMap.put("claimNum", sum);
			
			//保全
		 	resultMap.put("acceptNum", acceptingService.getAcceptNums(Long.parseLong(user.getAgentId())));

		 	//续期待收
			sum = 0;
			result = contractsService.getOcpNum(Long.parseLong(user.getAgentId()));
			for(Map<String, Object> map : result) {
				sum += Integer.parseInt(map.get("NUM").toString());
			}
		 	resultMap.put("ocpPolicyNum", sum);
			
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取各种保单的数量失败,请重试");
		}
	}
	
	/**
	 * 获取新单追踪保单数量
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/findNewOrdersNum.action
	 */
	@RequestMapping(value = "findNewOrdersNum")
	@ResponseBody
	public Map<String, Object> findNewOrdersNum(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap.put("newOrdersNum", myPolicyService.getOrdersNum(Long.parseLong(user.getAgentId())));
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取新单追踪保单数量失败,请重试");
		}
	}
	
	/**
	 * 获取承保前保单数量
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/findUnderwriteNums.action
	 */
	@RequestMapping(value = "findUnderwriteNums")
	@ResponseBody
	public Map<String, Object> findUnderwriteNums(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap.put("underwriteNum", beforeUnderwritingService.getUnderwriteNums(Long.parseLong(user.getAgentId())));
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取承保前保单数量失败,请重试");
		}
	}
	
	/**
	 * 获取理赔保单数量
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/findClaimNums.action
	 */
	@RequestMapping(value = "findClaimNums")
	@ResponseBody
	public Map<String, Object> findClaimNums(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			int sum = 0;
		 	List<Map<String, Object>> result = claimService.getClaimChart(Long.parseLong(user.getAgentId()));
		 	for(Map<String, Object> map : result) {
				sum += Integer.parseInt(map.get("NUM").toString());
			}
		 	resultMap.put("claimNum", sum);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取理赔保单数量失败,请重试");
		}
	}
	
	/**
	 * 获取保全保单数量
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/findAcceptNums.action
	 */
	@RequestMapping(value = "findAcceptNums")
	@ResponseBody
	public Map<String, Object> findAcceptNums(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap.put("acceptNum", acceptingService.getAcceptNums(Long.parseLong(user.getAgentId())));
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取保全保单数量失败,请重试");
		}
	}
	
	/**
	 * 获取续期待收保单数量
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/findOcpNums.action
	 */
	@RequestMapping(value = "findOcpNums")
	@ResponseBody
	public Map<String, Object> findOcpNums(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			int sum = 0;
		 	List<Map<String, Object>> result = contractsService.getOcpNum(Long.parseLong(user.getAgentId()));
		 	for(Map<String, Object> map : result) {
				sum += Integer.parseInt(map.get("NUM").toString());
			}
		 	resultMap.put("ocpPolicyNum", sum);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取续期待收保单数量失败,请重试");
		}
	}

	/**
	 * 获取电子发票信息
	 * @param jsonParam
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getEInvoiceList")
	@ResponseBody
	public List<Map<String, Object>> getEInvoiceList(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception{
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try{
			List<Map<String, Object>> resultMap = myPolicyService.getEInvoiceList(Long.parseLong(map.get("contractId").toString()));
			return resultMap;
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取电子发票信息失败,请重试");
		}
	}
	
	/**
	 * 获取电子发票信息
	 * @param jsonParam
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "sendEmail")
	@ResponseBody
	public boolean sendEmail(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception{
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		String pdfUrl = map.get("pdfUrl")!=null?map.get("pdfUrl").toString():"";
		String receiveEmailAddress = map.get("receiveEmailAddress")!=null?map.get("receiveEmailAddress").toString():"";
		String sendEmailAddress = map.get("sendEmailAddress")!=null?map.get("sendEmailAddress").toString():"";
		Assert.notNull(receiveEmailAddress, "收件人邮箱不能为空");
		Assert.notNull(sendEmailAddress, "发件人邮箱不能为空");
		boolean result = EInvoiceMail.sendEmail(pdfUrl,receiveEmailAddress,sendEmailAddress);
		return result;
	}

	
	/**
	 * 万能账户信息
	 * @param jsonParam
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getSuperUser.action
	 */
	@RequestMapping(value = "getSuperUser")
	@ResponseBody
	public List<Map<String, Object>> getSuperUser(@RequestParam(value = "jsonParam") String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try {
			return myPolicyService.getSuperUser(Long.parseLong(map.get("contractId").toString()));
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取万能账户信息失败,请重试");
		}
	}
	
	/**
	 * 判断是否显示万能账户信息
	 * @param jsonParam
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/isShowSuper.action
	 */
	@RequestMapping(value = "isShowSuper")
	@ResponseBody
	public List<Map<String, Object>> isShowSuper(@RequestParam(value = "jsonParam") String jsonParam) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		try {
			return myPolicyService.isShowSuper(Long.parseLong(map.get("contractId").toString()));
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取失败,请重试");
		}
	}

}
